import 'package:flutter/material.dart';
import 'package:flutter_ohos/app/modules/counter_method_channel.dart';
import 'package:flutter_ohos/app/routes/app_routes.dart';
import 'package:get/get.dart';
import 'model/fourth_data_entity.dart';

import 'fourth_state.dart';

class FourthProvider extends ChangeNotifier {
  final state = FourthState();

  void initData() {
    getParams();
    fetchAndSetData();
  }
  
  getParams() {
    Map? parames = Get.arguments;
    if (parames != null) {
      if (parames["title"] != null) {
        state.title = parames["title"];
        if (hasListeners) {
          notifyListeners();
        }
      }
    }
  }
  

  Future<void> fetchAndSetData() async {
    // 假设你有一个获取数据的函数
    final data = await fetchData();
    state.student = data;
    print(state.student.name);
    if (hasListeners) {
      notifyListeners();
    }
  }

  Future<FourthDataEntity> fetchData() async {
    // 这里模拟网络请求，实际中替换为你的网络请求代码
    return Future.delayed(Duration(seconds: 2), () {
      return FourthDataEntity.fromJson({"name":"李四","age":14});
    });
  }

  void jumpToFive() {
    Get.toNamed(Routes.FIVEVIEW);
  }

  changeName() {
    if (state.student.name == "王五") {
      state.student.name = "张三";
    } else {
      state.student.name = "王五";
    }
    notifyListeners();
  }

  ///测试跳转华为账号登录页
  void loginAction() {
    //点击登录调取鸿蒙原生登录页面
    Channel.methodChannel
        .invokeMethod<String?>('login');
    Get.offNamedUntil(Routes.INDEX, (route) => false);
  }

}
